package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;
import java.util.List;

public class ClientPage extends Utils {
    public static String url = "http://127.0.0.1:8080/client.html";
    public static String res = "";

    public ClientPage() {
        super(url);
    }

    //检查页面是否加载成功
    public void ClientPageRight() throws InterruptedException, IOException {
        //通过查看页面元素是否存在来检查页面加载成功与否
        res = driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.user")).getText();
        getScreenShot(getClass().getName());
        assert res.equals("lisi");
    }

    //检查基本控件
    public void BasicTest() throws IOException {
        //搜索输入框正常输入
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.search > input[type=text]")).sendKeys("111");
        res = driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.search > input[type=text]")).getText();
        assert res.equals("111");
        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.search > input[type=text]")).clear();

        //聊天框输入正常
        //从聊天列表中点击好友
        driver.findElement(By.cssSelector("#session-list > li:nth-child(1) > h3")).click();
        String open = driver.findElement(By.cssSelector("#session-list > li:nth-child(1) > h3")).getText();
        getScreenShot(getClass().getName());
        assert open.equals(driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText());

        //输入消息
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).sendKeys("111");
        res = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).getText();
        assert res.equals("111");
        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).clear();

        //点击切换好友列表
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend")).click();
        res = driver.findElement(By.cssSelector("#session-list")).getAttribute("class");
        getScreenShot(getClass().getName());
        assert res.equals("list hide");

        //点击切换聊天列表
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-session")).click();
        res = driver.findElement(By.cssSelector("#friend-list")).getAttribute("class");
        getScreenShot(getClass().getName());
        assert res.equals("list hide");
    }

    //发送信息测试
    public void SendMessage() throws IOException, InterruptedException {
        //从聊天列表中点击好友
        driver.findElement(By.cssSelector("#session-list > li:nth-child(1) > h3")).click();
        String open = driver.findElement(By.cssSelector("#session-list > li:nth-child(1) > h3")).getText();
        getScreenShot(getClass().getName());
        assert open.equals(driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText());

        //发送消息
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).sendKeys("聊天列表自动化测试");
        getScreenShot(getClass().getName());
        // 定位目标元素
        WebElement target = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button"));
        Thread.sleep(3000);
        Actions subAction = new Actions(driver);
        // 创建 Actions 对象
        subAction.moveToElement(target).perform();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button")).click();

        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
        getScreenShot(getClass().getName());
        // 等待消息出现并获取最后一条消息
        List<WebElement> messages = wait.until(driver -> {
            driver.findElement(By.cssSelector("#session-list > li:nth-child(1) > h3")).click();
            List<WebElement> elements = driver.findElements(
                    By.cssSelector("body > div.client-container > div > div.right > div.message-show > div")
            );
            return elements.isEmpty() ? null : elements;  // 返回null会继续等待
        });

        // 获取消息数量
        int num = messages.size();

        // 处理最后一条消息
        if (num > 0) {
            WebElement lastMessage = messages.get(num - 1);
            getScreenShot(getClass().getName());
            assert lastMessage.getText().equals("聊天列表自动化测试");
        }

        driver.navigate().refresh();

        //从好友列表中点击好友
        //进入好友列表#friend-list > li > h4
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend")).click();
        Thread.sleep(500);
        driver.findElement(By.cssSelector("#friend-list > li > h4")).click();
        open = driver.findElement(By.cssSelector("#friend-list > li > h4")).getText();
        getScreenShot(getClass().getName());
        assert open.equals(driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.title")).getText());

        //发送消息
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > textarea")).sendKeys("好友列表自动化测试");
        getScreenShot(getClass().getName());
        // 定位目标元素
        target = driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button"));
        Thread.sleep(3000);
        subAction = new Actions(driver);
        // 创建 Actions 对象
        subAction.moveToElement(target).perform();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        driver.findElement(By.cssSelector("body > div.client-container > div > div.right > div.ctrl > button")).click();

        wait = new WebDriverWait(driver, Duration.ofSeconds(30));
        getScreenShot(getClass().getName());
        // 等待消息出现并获取最后一条消息
        messages = wait.until(driver -> {
            driver.findElement(By.cssSelector("#session-list > li:nth-child(1) > h3")).click();
            List<WebElement> elements = driver.findElements(
                    By.cssSelector("body > div.client-container > div > div.right > div.message-show > div")
            );
            return elements.isEmpty() ? null : elements;  // 返回null会继续等待
        });

        // 获取消息数量
        num = messages.size();

        // 处理最后一条消息
        if (num > 0) {
            WebElement lastMessage = messages.get(num - 1);
            getScreenShot(getClass().getName());
            assert lastMessage.getText().equals("好友列表自动化测试");
        }

    }

    public void pageNoLogin() throws IOException {
        Alert alert = driver.switchTo().alert();
        res = alert.getText();
        assert res.equals("当前用户未登录!");

        alert.accept();
        res = driver.getCurrentUrl();
        assert res.equals("http://127.0.0.1:8080/login.html");
    }
}
